package com.example.lowbird.lowbird.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.lowbird.lowbird.R;
import com.example.lowbird.lowbird.fragment.ShopingFragment;
import com.example.lowbird.lowbird.java.SHopcar;
import com.example.lowbird.lowbird.utils.Animatorutils;
import com.example.lowbird.lowbird.utils.Dbutils;
import com.example.lowbird.lowbird.view.Shoptext;

import java.util.IdentityHashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/11/3.
 */
public class Shopadapter extends BaseAdapter {

    private Context mContext;

    private LayoutInflater inflater;

    private List<SHopcar> shopcars;
    boolean flag = true;

    public Shopadapter(Context mContext, List<SHopcar> shopcars) {
        this.mContext = mContext;

        inflater = LayoutInflater.from(mContext);
        this.shopcars = shopcars;
    }

    @Override
    public int getCount() {
        return shopcars == null ? 0 : shopcars.size();
    }

    @Override
    public Object getItem(int position) {
        return shopcars.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        Log.e("SHOP", "getView: " + shopcars.size());
        ViewHolder holder = null;
        final SHopcar sHopcar = shopcars.get(position);

        if (convertView == null) {

            convertView = inflater.inflate(R.layout.adapter_shopcar, null);

            holder = new ViewHolder(convertView);

            convertView.setTag(holder);


        } else {

            holder = ((ViewHolder) convertView.getTag());

        }


        final ViewHolder finalHolder = holder;
        holder.checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                boolean checked = finalHolder.checkbox.isChecked();
                Dbutils.updataCheck(shopcars.get(position).getName(), checked == true ? 2 : 1);
                Log.e("SHOP", "onClick: " + position);
            }
        });


        int selected = sHopcar.getSelected();
        Log.e("SHOP", "getView: " + selected);
        holder.checkbox.setChecked(selected == 1 ? false : true);
        Glide.with(mContext).load(sHopcar.getImageurl()).diskCacheStrategy(DiskCacheStrategy.ALL)
                .crossFade().into(holder.imiageview);

        holder.tv_name.setText(sHopcar.getName());
        holder.tv_price.setText("¥ " + sHopcar.getPrice() + "");
        holder.shoptext.setMaxsize(sHopcar.getMaxsize());
        holder.shoptext.setTextcount(sHopcar.getCount());
        final int textcount = holder.shoptext.getTextcount();
        holder.shoptext.setCallback(new Shoptext.ShopcarInterface() {
            @Override
            public void addcunt(int lastcount) {

                Dbutils.updata(sHopcar.getName(), lastcount);


            }

            @Override
            public void delecount(int lastcount) {
                Dbutils.updata(sHopcar.getName(), lastcount);


            }

            @Override
            public void deletitem() {
                Dbutils.deletoneShop(sHopcar.getName());
            }
        });
        final ViewHolder finalHolder1 = holder;
        View.OnClickListener listenser=new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Animatorutils.initanimation(mContext, finalHolder1.imiageview,sHopcar);
            }
        };
        holder.imiageview.setOnClickListener(listenser);
        holder.tv_name.setOnClickListener(listenser);
        return convertView;
    }

    public void changdata(List<SHopcar> shopscars) {
        this.shopcars = shopscars;
        notifyDataSetChanged();
        Log.e("SHOP", "changdata: " + shopscars.size());
    }

    class ViewHolder {


        private ImageView imiageview;


        TextView tv_name;


        TextView tv_price;


        Shoptext shoptext;

        CheckBox checkbox;

        public ViewHolder(View view) {
            imiageview = (ImageView) view.findViewById(R.id.iv_adapter_shop);
            tv_name = (TextView) view.findViewById(R.id.tv_shop_name);
            tv_price = (TextView) view.findViewById(R.id.tv_shopadapterprice);
            shoptext = (Shoptext) view.findViewById(R.id.st_shopcar);

            checkbox = ((CheckBox) view.findViewById(R.id.cb_adapter_shop));


        }
    }
}
